package com.hxzy.controller.admin;

import com.hxzy.common.annon.Log;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.enums.BusinessType;
import com.hxzy.common.vo.R;
import com.hxzy.dto.system.SysDictDataDTO;
import com.hxzy.entity.system.SysDictData;
import com.hxzy.redis.cache.DictDataCache;
import com.hxzy.service.system.SysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

@Api(tags = "数据字典值API")
@RestController
@RequestMapping(value = "/api/dictdata")
public class SysDictDataController  extends BaseController{

    /**
     * sys_dict_data数据库
     */
    @Autowired
    private SysDictDataService sysDictDataService;

    /**
     * sys_dict_data缓存
     */
    @Autowired
    private DictDataCache dictDataCache;


    @PreAuthorize(value = "@ss.hasAuthority('system:dict:query')")
    @ApiOperation(value = "分页获取字典数据列表")
    @GetMapping("/data")
    public R list(SysDictDataDTO sysDictDataDTO) {
        super.startPageHelper();
        List<SysDictData> page = this.sysDictDataService.selectDictDataList(sysDictDataDTO);
        return super.pageToPageVO(page);
    }



    @PreAuthorize(value = "@ss.hasAuthority('system:dict:query')")
    @ApiOperation(value = "根据字典类型查询")
    @GetMapping(value = "/type/{dictType}")
    @ApiImplicitParam(name = "dictType",value = "字典类型",required = true)
    public R  findByDictType(@PathVariable(value = "dictType") String dictType){
        List<SysDictData> byDictType = this.dictDataCache.findByDictType(dictType);
        return R.okHasData(byDictType);
    }

    @Log(title = "字典管理",businessType = BusinessType.INSERT)
    @PreAuthorize(value = "@ss.hasAuthority('system:dict:add')")
    @ApiOperation(value = "新增")
    @PostMapping
    public R insert(@RequestBody @Valid SysDictData  sysDictData){
          // 向数据库中写入数据
          boolean result=this.sysDictDataService.save(sysDictData);
          //同步redis的缓存
         this.dictDataCache.rewriteDictTypeValue(sysDictData.getDictType());
          return super.toR(result);
    }

    @Log(title = "字典管理",businessType = BusinessType.UPDATE)
    @PreAuthorize(value = "@ss.hasAuthority('system:dict:edit')")
    @ApiOperation(value = "修改字段数据")
    @PutMapping
    public R updateData(@RequestBody @Validated SysDictData sysDictData){
        boolean result= this.sysDictDataService.updateById(sysDictData);
        //同步redis的缓存
        this.dictDataCache.rewriteDictTypeValue(sysDictData.getDictType());
        return  super.toR(result);
    }

    @Log(title = "字典管理",businessType = BusinessType.DELETE)
    @PreAuthorize(value = "@ss.hasAuthority('system:dict:remove')")
    @ApiOperation(value = "删除字典数据")
    @DeleteMapping(value = "/{ids}")
    public R delType(   @PathVariable(value = "ids") Long[]  dictCode){
       boolean result= this.sysDictDataService.updateState(dictCode,"1");
        return  super.toR(result);
    }

    @PreAuthorize(value = "@ss.hasAuthority('system:dict:query')")
    @ApiOperation(value = "根据主键查询")
    @GetMapping("/{dictCode}")
    public R selectTypeId(@PathVariable Long dictCode) {
        SysDictData dictData = this.sysDictDataService.getById(dictCode);
        if (Objects.isNull(dictData)){
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        dictData.setUpdateBy(null);
        dictData.setUpdateTime(null);
        dictData.setCreateBy(null);
        dictData.setCreateTime(null);
        return R.okHasData(dictData);
    }

}
